/*******************************************************************************
 * 文件名: MoneyFormat.java
 * 描    述: 金额转换类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2018-09-15
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.format;

public class MoneyFormat {

	private static final String[] minUnits = {" 分", " 角"};
	private static final String[] maxUnits = {" 元", " 万", " 亿"};
	private static final String[] stageUnits = {" ", " 拾", " 佰", " 仟"};
	
	/**
	 * 根据数字获取对应中文
	 * @param num
	 * @return
	 */
	public static String getNumUpperCase(int num) {
		switch (num) {
		case 0:return " 零";
		case 1:return " 壹";
		case 2:return " 贰";
		case 3:return " 叁";
		case 4:return " 肆";
		case 5:return " 伍";
		case 6:return " 陆";
		case 7:return " 柒";
		case 8:return " 捌";
		case 9:return " 玖";
		default : return "";
		}
	}
	
	/**
	 * 大写金额，单位:元
	 * @param num
	 * @param max
	 * @return
	 */
	public static String getMoneyUpperCase(double num) {
		return getMoneyUpperCase(num, -1);
	}
	public static String getMoneyUpperCase(double num, int unitLevel) {
		StringBuffer moneyBuf = new StringBuffer();
		//小单位(分,角)
		for (int i = 0; i < minUnits.length; i++) {
			int k = (int)(num * Math.pow(10, minUnits.length - i) % 10);
			moneyBuf.append(minUnits[i]).append(getNumUpperCase(k));
		}
		//大单位（元以上单位）
		double t = num;
		e1:for (int i = 0; i < maxUnits.length; i++) {
			for (int j = 0; j < stageUnits.length; j++) {
				if (unitLevel > 0 && unitLevel < (i + 1) * (j + 1)) {
					break e1;
				}
				if (0 == j) { moneyBuf.append(maxUnits[i]); }
				else { moneyBuf.append(stageUnits[j]); }
				int k = (int)(t % 10);
				t /= 10;
				moneyBuf.append(getNumUpperCase(k));
			}
		}
		return moneyBuf.reverse().toString();
	} 
}
